﻿/*----------------------------------------------------------------
//   Copyright (C) 2012 YMSOFT All rights reserved 
//
//   CLR版本:   2.0.50727.3634
//   Namespace: ART.Common
//   FileName : JEnum
//   Description: 
// 
//   Created by YAOMING at 2012-11-4 20:27:58
//   http://www.loverer.com QQ:78867011
//
//   Modified: 
//   Modify description: 
//
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ART.Common
{
   public class JEnum
    {
       private static DataTable CreateDB()
       {
           DataTable DT = new DataTable();
           DT.Columns.Add("ID",typeof(int));
           DT.Columns.Add("Name",typeof(string));
           return DT;
       }
       /// <summary>
       /// 枚举枚举值返回DataTable
       /// </summary>
       /// <param name="enumType"></param>
       /// <returns></returns>
       public static DataTable GetList(Type enumType)
       {
           DataTable dt = CreateDB();
           foreach (int myCode in Enum.GetValues(enumType))
             {
                 string strName = Enum.GetName(enumType, myCode);//获取名称
                 string strVaule = myCode.ToString();//获取值
                 DataRow row = dt.NewRow();
                 row["id"] = strVaule;
                 row["name"] = strName;
                 dt.Rows.Add(row);
             }
           return dt;
       }
       /// <summary>
       /// 枚举枚举值返回DataTable ,排除指定的枚举
       /// </summary>
       /// <param name="enumType"></param>
       /// <param name="nolist"></param>
       /// <returns></returns>
       public static DataTable GetList(Type enumType,string nolist)
       {
           DataTable dt = CreateDB();
           foreach (int myCode in Enum.GetValues(enumType))
           {
               if ((","+nolist+",").IndexOf(","+myCode+",")==-1)
               {
                   string strName = Enum.GetName(enumType, myCode);//获取名称
                   string strVaule = myCode.ToString();//获取值
                   DataRow row = dt.NewRow();
                   row["id"] = strVaule;
                   row["name"] = strName;
                   dt.Rows.Add(row);
               }
              
           }
           return dt;
       }
       /// <summary>
       /// 获取指定枚举值的常数名称
       /// </summary>
       /// <param name="enumType"></param>
       /// <param name="i"></param>
       /// <returns></returns>
       public static string GetString(Type enumType, int i)
       {
           string strName="";
           if (Enum.IsDefined(enumType,i))
           {
               strName = Enum.GetName(enumType, i);
           }
           return strName;
       }
       /// <summary>
       /// 获取指定常数名称的枚举值
       /// </summary>
       /// <param name="enumType"></param>
       /// <param name="Name"></param>
       /// <returns></returns>
       public static int GetInt(Type enumType, string Name)
       {
           int i = -100;
           foreach (int myCode in Enum.GetValues(enumType))
           {
               string strName = Enum.GetName(enumType, myCode);//获取名称
               if (strName==Name)
               {
                   i = myCode;
                   break;
               }
           }
           return i;
       }
    }
}
